#pragma once

#include "zrpc/http/http_request.h"
#include "zrpc/http/http_response.h"
namespace zrpc {
class Buffer;

class HttpContext {
 public:
  enum HttpRequestParseState {
    kExpectRequestLine,
    kExpectHeaders,
    kExpectBody,
    kGotAll,
  };

  HttpContext() : state_(kExpectRequestLine) {}

  bool ParseRequest(Buffer *buf);
  bool ParseResponse(Buffer *buf);
  bool GotAll() const { return state_ == kGotAll; }

  void Reset() {
    state_ = kExpectRequestLine;
    HttpRequest dummy;
    request_.Swap(dummy);
  }

  HttpRequest &GetRequest() { return request_; }
  HttpResponse &GetResponse() { return response_; }
  bool ProcessRequestLine(const char *begin, const char *end);
  bool ProcessResponseLine(const char *begin, const char *end);

 private:
  HttpRequestParseState state_;
  HttpRequest request_;
  HttpResponse response_;
};
}  // namespace zrpc
